package com.nantaphop.hovertouchview;

import ohos.agp.animation.Animator;
import ohos.agp.components.Component;
import ohos.agp.components.StackLayout;
import ohos.multimodalinput.event.TouchEvent;

/**
 * HoverTouchHelper
 */
public class HoverTouchHelper {
    /**
     * HoverTouchHelper
     *
     * @param root for root layout
     * @param hoverTouchAble for picture surface
     */
    public static void make(StackLayout root, HoverTouchAble hoverTouchAble) {
        if (hoverTouchAble instanceof Component) {
            final Component rootComponent = (Component) hoverTouchAble.getHoverView();
            final Component src = (Component) hoverTouchAble;
            root.addComponent(rootComponent);
            rootComponent.setScaleX(0.3f);
            rootComponent.setScaleY(0.3f);
            root.removeComponent(rootComponent);
            final int duration = hoverTouchAble.getHoverAnimateDuration();

            src.setTouchEventListener(new Component.TouchEventListener() {
                Component hoverView = rootComponent;
                Component sourceView = src;

                @Override
                public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
                    switch (touchEvent.getAction()) {
                        case TouchEvent.PRIMARY_POINT_DOWN:
                            root.setAlpha(0.3f);
                            hoverTouchAble.onStartHover();

                            // Blurry third party lib not available. Below line will be ported when Blurry lib available.
                            // Blurry.with(root.getContext()).radius(8).sampling(2).
                            // capture(root).into(HoverTouchHelper.addBlurView(root));

                            if (hoverView.getComponentParent() == null) {
                                root.removeComponent(hoverView);
                                root.addComponent(hoverView);
                            }
                            hoverView.createAnimatorProperty().scaleX(1).scaleY(1).alpha(1).setStateChangedListener(null).setDuration(duration).start();
                            break;

                        case TouchEvent.PRIMARY_POINT_UP:
                            root.setAlpha(1.0f);

                            // Blurry third party lib not available. Below line will be ported when Blurry lib available.
                            // HoverTouchHelper.removeBlurView(root, duration);

                            hoverView.createAnimatorProperty().cancel();
                            hoverView.createAnimatorProperty().scaleX(0).scaleY(0).alpha(0)
                                    .setDuration(duration)
                                    .setStateChangedListener(new Animator.StateChangedListener() {
                                        @Override
                                        public void onStart(Animator animator) {
                                        }

                                        @Override
                                        public void onStop(Animator animator) {
                                        }

                                        @Override
                                        public void onCancel(Animator animator) {
                                        }

                                        @Override
                                        public void onEnd(Animator animator) {
                                            root.removeComponent(hoverView);
                                            hoverTouchAble.onStopHover();
                                        }

                                        @Override
                                        public void onPause(Animator animator) {
                                        }

                                        @Override
                                        public void onResume(Animator animator) {
                                        }

                                    })
                                    .start();
                            break;
                        default:
                            break;
                    }
                    return true;
                }
            });
        } else {
            throw new IllegalArgumentException("HoverTouchAble must be a Component");
        }
    }
}